package com.microcity.autocomponent.autoview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.microcity.autocomponent.R;

/**
 * Created by chenliu on 2017/3/10 0010<br/>.
 * 描述：
 */
public class BikeProgressBar extends View {

    private Paint paint;
    private Bitmap bikeIcon;
    private int borderPadding = 10;
    private RectF rectF;
    private int height;
    private int progress;

    public BikeProgressBar(Context context) {
        this(context, null, 0);
    }

    public BikeProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BikeProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#aaffffff"));
        paint.setStyle(Paint.Style.FILL);
        BitmapFactory.Options options = new BitmapFactory.Options();
        bikeIcon = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_ubike_unlock_progress_icon, options);
        borderPadding = dp2px(10);
        height = options.outHeight + borderPadding;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(widthSpecSize, height);
        rectF = new RectF(0, 0,getMeasuredWidth(), getMeasuredHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(rectF, 100f, 100f, paint);
        float left = borderPadding / 2f + progress / 100f * (getMeasuredWidth() - bikeIcon.getWidth() - borderPadding);
        canvas.drawBitmap(bikeIcon, left, borderPadding / 2f, null);
    }

    public void setmProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    private int dp2px(int dp){
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density);
    }
}
